package kr.pe.johnkim.carrotpoint.mobile.controller;

import kr.pe.johnkim.carrotpoint.mobile.domain.CarrotPointMerchant;
import kr.pe.johnkim.carrotpoint.mobile.service.CarrotPointMerchantService;
import kr.pe.johnkim.carrotpoint.mobile.util.CarrotPointCommon;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Carrot Point 가맹점 관련 controller 클래스 이다.
 *
 * @author : John_Kim <cale9797@gmail.com>
 * @date : 13. 2. 6.
 */
@Controller
@RequestMapping(value = "/carrotPointMerchantController")
public class CarrotPointMerchantController {

    // SLF4J Logging
    private Logger logger = LoggerFactory.getLogger(CarrotPointMerchantController.class);

    @Autowired
    private CarrotPointMerchantService carrotPointMerchantService;

    @Autowired
    private CarrotPointCommon carrotPointCommon;

    /**
     * 최초 등록 시 가맹점 아이디로 중복 검사를 한다.
     * 한번 등록된 가맹점 아이디는 activateYn 의 값에 상관없이 재등록 할 수 없다.
     *
     * @param carrotPointMerchant
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/selectMerchantExistById", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public CarrotPointMerchant selectMerchantExistById
            (@RequestBody CarrotPointMerchant carrotPointMerchant) throws Exception {
        logger.info("getMerchantExistById");

        logger.info("merchantId : {}", carrotPointMerchant.getMerchantId());

        return carrotPointMerchantService.getMerchantExistById(carrotPointMerchant);
    }

    /**
     * 가맹점을 등록 한다.
     *
     * @param carrotPointMerchant
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/registerMerchant", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public CarrotPointMerchant registerMerchant
            (@RequestBody CarrotPointMerchant carrotPointMerchant) throws Exception {
        logger.info("registerMerchant");

        // 비밀번호를 암호화 한다.
        CarrotPointMerchant parameter = new CarrotPointMerchant();
        parameter.setPassword(carrotPointCommon.getEncodedPassword(carrotPointMerchant.getPassword()));
        carrotPointMerchant.setPassword(parameter.getPassword());

        CarrotPointMerchant result = new CarrotPointMerchant();

        try {
            carrotPointMerchantService.registerMerchant(carrotPointMerchant);
            result.setResult("SUCCESS");
            logger.info("success to register merchant.");
        } catch (Exception e) {
            result.setResult("FAiL");
            logger.info("fail to register merchant.");
        }

        return result;
    }

    /**
     * 서비스 로그인을 한다.
     *
     * @param carrotPointMerchant
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public CarrotPointMerchant login
            (@RequestBody CarrotPointMerchant carrotPointMerchant) throws Exception {
        logger.info("login");

        logger.info("merchantId : {}", carrotPointMerchant.getMerchantId());
        logger.info("password   : {}", carrotPointMerchant.getPassword());

        CarrotPointMerchant parameter = new CarrotPointMerchant();
        parameter.setPassword(carrotPointCommon.getEncodedPassword(carrotPointMerchant.getPassword()));
        carrotPointMerchant.setPassword(parameter.getPassword());

        logger.info("password   : {}", carrotPointMerchant.getPassword());

        return carrotPointMerchantService.login(carrotPointMerchant);
    }

    /**
     * 가맹점 정보를 가져 온다.
     *
     * @param carrotPointMerchant
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/selectMerchantInformation", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public CarrotPointMerchant selectMerchantInformation
            (@RequestBody CarrotPointMerchant carrotPointMerchant) throws Exception {
        logger.info("getMerchantInformation");

        logger.info("merchantId : {}", carrotPointMerchant.getMerchantId());
        logger.info("activateYn : {}", carrotPointMerchant.getActivateYn());

        return carrotPointMerchantService.getMerchantInformation(carrotPointMerchant);
    }

    /**
     * 가맹점 정보를 업데이트 한다.
     *
     * @param carrotPointMerchant
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateMerchantInformation", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public CarrotPointMerchant updateMerchantInformation
            (@RequestBody CarrotPointMerchant carrotPointMerchant) throws Exception {
        logger.info("updateMerchantInformation");

        CarrotPointMerchant result = new CarrotPointMerchant();

        try {
            carrotPointMerchantService.updateMerchantInformation(carrotPointMerchant);
            result.setResult("SUCCESS");
            logger.info("success to update merchant information.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.info("fail to update merchant information.");
        }

        return result;
    }

    /**
     * 가맹점의 서비스 사용 제한 여부를 업데이트 한다.
     *
     * @param carrotPointMerchant
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateRestrictionMerchant", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public CarrotPointMerchant updateRestrictionMerchant
            (@RequestBody CarrotPointMerchant carrotPointMerchant) throws Exception {
        logger.info("updateRestrictionMerchant");

        CarrotPointMerchant result = new CarrotPointMerchant();

        try {
            carrotPointMerchantService.updateRestrictionMerchant(carrotPointMerchant);
            result.setResult("SUCCESS");
            logger.info("success to update restriction merchant.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.info("fail to update restriction merchant.");
        }

        return result;
    }

    /**
     * 가맹점 위치 정보를 업데이트 한다.
     *
     * @param carrotPointMerchant
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateMerchantLocation", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public CarrotPointMerchant updateMerchantLocation
            (@RequestBody CarrotPointMerchant carrotPointMerchant) throws Exception {
        logger.info("updateMerchantLocation");

        CarrotPointMerchant result = new CarrotPointMerchant();

        try {
            carrotPointMerchantService.updateMerchantLocation(carrotPointMerchant);
            result.setResult("SUCCESS");
            logger.info("success to update merchant location.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.info("fail to update merchant location.");
        }

        return result;
    }

    /**
     * 가맹점 검색 목록을 가져 온다. 상호 명과 업종으로 검색 한다.
     *
     * @param carrotPointMerchant
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/selectSearchMerchantList", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public List<CarrotPointMerchant> selectSearchMerchantList
            (@RequestBody CarrotPointMerchant carrotPointMerchant) throws Exception {
        logger.info("getSearchMerchantList");

        logger.info("companyName    : {}", carrotPointMerchant.getCompanyName());
        logger.info("businessKind03 : {}", carrotPointMerchant.getBusinessKind03());

        return carrotPointMerchantService.getSearchMerchantList(carrotPointMerchant);
    }

    /**
     * 가맹점 활성화 여부를 업데이트 한다.
     *
     * @param carrotPointMerchant
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateMerchantActivateYn", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public CarrotPointMerchant updateMerchantActivateYn
            (@RequestBody CarrotPointMerchant carrotPointMerchant) throws Exception {
        logger.info("updateMerchantActivateYn");

        CarrotPointMerchant result = new CarrotPointMerchant();

        try {
            carrotPointMerchantService.updateMerchantActivateYn(carrotPointMerchant);
            result.setResult("SUCCESS");
            logger.info("success to update merchant activateYn.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.info("fail to update merchant activateYn.");
        }

        return result;
    }
}
